package cn.bulibuqi.myblog.controller;

import cn.bulibuqi.myblog.bean.Type;
import cn.bulibuqi.myblog.service.TypeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * @author bulibuqi
 * @create 2021-12-10:49
 */
@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;


    @GetMapping("/types/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        model.addAttribute("type",typeService.getType(id));

        return "admin/types-input";
    }

    @GetMapping("/types")
    public String list(
               @RequestParam(required = false, defaultValue = "1") Integer current,  //必须String，框架会自动转换
               @RequestParam(required = false, defaultValue = "10") Integer size,
               Model model){


        Page page = new Page(current,size);
        Page<Type> typePage = typeService.listType(page);
        model.addAttribute("page",typePage);
        return "admin/types";
    }
    @GetMapping("/types/input")
    public String input(Model model){

        model.addAttribute("type",new Type());
        return "admin/types-input";
    }

    @PostMapping("/types")
    public String typesPost(@Valid Type type,//@Valid 为页面效验注解
                            BindingResult result, //校验接收的结果
                            RedirectAttributes attributes){


        Type type1 = typeService.getTypeByName(type.getName());
        if (type1!=null){
            result.rejectValue("name","nameError","不能添加重复分类");
        }

        if(result.hasErrors()){
            return "admin/types-input";
        }

        boolean t = typeService.saveType(type);
        if (t==false){
            attributes.addFlashAttribute("message","新增失败");

        }else {
            attributes.addFlashAttribute("message","新增成功");

        }
        return "redirect:/admin/types";


    }


    @PostMapping("/types/{id}")
    public String editPost(@Valid Type type,//@Valid 为页面效验注解
                            BindingResult result, //校验接收的结果
                            @PathVariable Long id,
                            RedirectAttributes attributes){


        Type type1 = typeService.getTypeByName(type.getName());
        if (type1!=null){
            result.rejectValue("name","nameError","不能添加重复分类");
        }

        if(result.hasErrors()){
            return "admin/types-input";
        }

        boolean t = typeService.updateType(id,type);
        if (t==false){
            attributes.addFlashAttribute("message","更新失败");

        }else {
            attributes.addFlashAttribute("message","更新成功");

        }
        return "redirect:/admin/types";


    }

    @GetMapping("/types/{id}/{current}/delete")
    public String delete(@PathVariable Long id,@PathVariable Integer current, RedirectAttributes attributes){


        try{
            typeService.deleteType(id);

            attributes.addFlashAttribute("message","删除成功");
            //给跳转的页面加入参数
            attributes.addAttribute("current",current);
        }catch (Exception e){

            attributes.addFlashAttribute("message","删除失败");
            //给跳转的页面加入参数
            attributes.addAttribute("current",current);
        }

        return "redirect:/admin/types";
    }




}
